const fs = require('fs');
const proxy = require('http-proxy');
const colors = require('colors-console');
const configTool = require('../../com/configTool.js');


function httpsProxy() {
    const clientServerPort = configTool.config.clientServerPort;
    const connectServeHTTPSrPort = configTool.config.HTTPSProxy.port;
    if (connectServeHTTPSrPort) {
        proxy.createServer({
            xfwd: true,
            ws: true,
            target: {
                host: 'localhost',
                port: clientServerPort
            },
            ssl: {
                key: fs.readFileSync(configTool.appBasePath + configTool.config.HTTPSProxy.keyPath, 'utf8'),
                cert: fs.readFileSync(configTool.appBasePath + configTool.config.HTTPSProxy.certPath, 'utf8')
            }
        }).on('error', function (e:any) {
            console.log(colors('red', `【启动 HTTPS Web服务器 失败】请检查是否全局安装 npm i local-ssl-proxy -g ，或端口号：${connectServeHTTPSrPort} 被占用`));
        }).listen(connectServeHTTPSrPort);

        console.log(`【启动 HTTPS Web服务器 - 自签名】：URL:【https://localhost:${connectServeHTTPSrPort}】`);
    }
}


export =  httpsProxy;
